package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.LocalDao;
import edu.upc.clase.demo.entity.Local;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author gian
 */
@Repository
public class LocalDaoImpl extends SimpleJdbcDaoSupport implements LocalDao {

    private static Logger log = LoggerFactory.getLogger(LocalDaoImpl.class);

    @Autowired
    public LocalDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Local local) {

        getJdbcTemplate().update(
                "insert into local (local) values (?)",
                 local.getLocal());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
    }

    @Override
    public void actualizar(Local local) {
        getJdbcTemplate().update(
                "update local set local = ? where id_local = ? ",
                local.getLocal(), local.getIdLocal());
    }

    @Override
    public void eliminar(Local local) {
        getJdbcTemplate().update(
                "delete from local where id_local = ?", local.getIdLocal());
    }

        @Override
    public List<Local> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "select idLocal, local from local",
                new BeanPropertyRowMapper<Local>(Local.class));
    }
}